<template>
  <el-dialog :visible.sync="value" :before-close="cancel">
    <template slot="title">
      风控审核
    </template>
    <el-form :model="form" ref="withdrawForm" :rules="withdrawRule" :label-width="formLabelWidth">
      <div style="padding: 10px 0 10px 150px;color: #000;">
        您选择了{{ids.length}}条记录，确定要{{step == 'agree'?'':'拒绝'}}通过吗？
      </div>
      <el-form-item label="理由：" prop="rejectReason" v-if="step !== 'agree'">
        <el-col :span="16">
          <el-select v-if="step == 'refuse'" v-model="form.rejectReason" placeholder="请选择" size="medium">
            <el-option
              v-for="(item,index) in refuseReasons"
              :key="index"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="备注：" prop="remark" >
        <el-col :span="16">
          <el-input type="textarea" v-model="form.remark"></el-input>
          <el-tip v-if="step == 'agree'">*确认后财务将进行提款处理</el-tip>
          <el-tip v-else-if="step == 'refuse'">*确认后提现金额将返回用户账户中</el-tip>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="doSave({action: 'saveForm', form: 'withdrawForm'})" :loading="loading">确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import { form } from '@/build';
import { updateWithdrawStatus, withdrawRefuseReasonList } from '@/services/api/withdraw';

export default {
  name: 'exception-withdraw-dialog',
  props: ['value', 'ids', 'step', 'menuIds'],
  mixins: [form],
  data() {
    return {
      formLabelWidth: '150px',
      refuseReasons: [],
      form: {
        status: this.step == 'agree' ? 1 : 2,
        orderId: this.ids.join(','),
        rejectReason: '',
        menuIds: this.menuIds.join(),
      },
      withdrawRule: {
        rejectReason: [
          { required: true, message: '请选择拒绝理由', trigger: 'blur' },
        ],
      },
      dataPermissionObj: {
        // 0: '正常提现',
        1: '首提金额过大',
        2: '单笔金额过大',
        3: '当日金额过大',
        4: '频繁提现',
        5: '利润异常',
        6: '7日累积金额过大',
        7: '提现IP冲突',
        8: '提现设备冲突',
      },
      failSumNum: 0,
    };
  },
  computed: {
    title() {
      switch (this.step) {
        case 'refuse':
          return '拒绝提现申请';
        case 'agree':
          return '提现申请';
        case 'back':
          return '退回';
      }
    },
  },

  mounted() {
    console.log('3454545',this.menuIds)
    this.getRefuseReasons();
    this.bindSave(updateWithdrawStatus, 'saveForm', 'form', {
      callback: ([result, data]) => {
        if (!result) {
          this.$notify({
            title: `保存失败.${data && data.message}`,
            type: 'error',
            duration: 2000,
          });
          return;
        }
        let html = '';
        if (this.step == 'agree') {
          if (data.refusePassButton.length) {
            _.each(Object.keys(data.refusePassButton[0]), (item) => {
              this.failSumNum += data.refusePassButton[0][item];
              html += `<div>${this.dataPermissionObj[item]}:${data.refusePassButton[0][item]}条</div>`;
            });
            html = `<div>你没有权限进行以下${this.failSumNum}条操作</div>${html}剩下${Number(this.ids.length) - this.failSumNum}条操作成功`;
            this.$alert(html, '提示', { dangerouslyUseHTMLString: true });
          }
        } else if (data.refuseRejectButton.length) {
          _.each(Object.keys(data.refuseRejectButton[0]), (item) => {
            this.failSumNum += data.refuseRejectButton[0][item];
            html += `<div>${this.dataPermissionObj[item]}:${data.refuseRejectButton[0][item]}条</div>`;
          });
          html = `<div>你没有权限进行以下${this.failSumNum}条操作</div>${html}剩下${Number(this.ids.length) - this.failSumNum}条操作成功`;
          this.$alert(html, '提示', { dangerouslyUseHTMLString: true });
        }
        this.cancel();
        this.$parent.getAll();

        if (Number(this.ids.length) - this.failSumNum > 0) {
          this.$notify({
            title: '保存成功',
            type: 'success',
            duration: 2000,
          });
        }
      },
    });
  },
  methods: {
    getRefuseReasons() {
      this.bindGet(withdrawRefuseReasonList, 'refuseReasons', {
        action: 'getRefuseReasons',
        format: (data) => {
          const refuseReasons = [];
          for (const item of data) {
            refuseReasons.push({
              value: item,
            });
          }
          if (this.step !== 'agree') {
            this.form.rejectReason = refuseReasons[0].value;
          }
          return refuseReasons;
        },
      });
      this.doGet({ action: 'getRefuseReasons' });
    },
    cancel() {
      this.$emit('input', false);
    },
  },

};
</script>

<style lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
